using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using IGGCore;


namespace IGG
{
    /// <summary>
    /// The many modes that the console IGG application can be set
    /// </summary>
    public class Modes
    {
        private static Dictionary<ConsoleKey, Mode> m_list = new Dictionary<ConsoleKey, Mode>();
        private static Mode m_currentMode;
        private static string m_helpResume = "";
        private static bool m_statsStringAdded = false;

        /// <summary>
        /// The current mode
        /// </summary>
        public static Mode CurrentMode
        {
            get { return Modes.m_currentMode; }
            set { Modes.m_currentMode = value; }
        }

        /// <summary>
        /// The complete modes help resume
        /// </summary>
        public static string HelpResume
        {
            get { return m_helpResume; }
        }

        /// <summary>
        /// Add a mode to the list and add it to the help resume
        /// </summary>
        private static void AddMode(ConsoleKey key, string name, IModeFormatter formatter)
        {
            if (key.ToString().Length == 2 && key.ToString()[0] == 'D')
            {
                if (m_statsStringAdded == false)
                {
                    m_helpResume += " [" + ToolBox.GetFormattedStringPadRight("0-9]", 6) + "Statistics" + Environment.NewLine;
                    m_statsStringAdded = true;
                }
            }
            else if (key.ToString()!= "M" && key.ToString()!= "Z")
            {
                m_helpResume += " [" + ToolBox.GetFormattedStringPadRight(key.ToString() + "]", 6) + name + Environment.NewLine;
            }
            if (key != ConsoleKey.F5)
            {
                m_list.Add(key, new Mode(name, key, formatter));
            }
        }

        /// <summary>
        /// Initialize the modes and assign them a command line formatter
        /// </summary>
        public static void Initialize()
        {
            AddMode(ConsoleKey.F1, "Help", new ModeHelpFormatter());
            AddMode(ConsoleKey.F2, "Day Before", null);
            AddMode(ConsoleKey.F3, "Day After", null);
            AddMode(ConsoleKey.F5, "Refresh", null);
            AddMode(ConsoleKey.S, "Standings", new ModeStandingsFormatter());
            AddMode(ConsoleKey.A, "Detailed Standings", new ModeDetailedStandingsFormatter());
            AddMode(ConsoleKey.D1, "Statistics 1", new ModeStatsFormatter(1));
            AddMode(ConsoleKey.D2, "Statistics 2", new ModeStatsFormatter(2));
            AddMode(ConsoleKey.D3, "Statistics 3", new ModeStatsFormatter(3));
            AddMode(ConsoleKey.D4, "Statistics 4", new ModeStatsFormatter(4));
            AddMode(ConsoleKey.D5, "Statistics 5", new ModeStatsFormatter(5));
            AddMode(ConsoleKey.D6, "Statistics 6", new ModeStatsFormatter(6));
            AddMode(ConsoleKey.D7, "Statistics 7", new ModeStatsFormatter(7));
            AddMode(ConsoleKey.D8, "Statistics 8", new ModeStatsFormatter(8));
            AddMode(ConsoleKey.D9, "Statistics 9", new ModeStatsFormatter(9));
            AddMode(ConsoleKey.D0, "Statistics 0", new ModeStatsFormatter(0));
            AddMode(ConsoleKey.B,  "Boxscore", new ModeBoxscoreFormatter());
            AddMode(ConsoleKey.D,  "Game Defense", new ModeDefenseFormatter());
            AddMode(ConsoleKey.F,  "Baseball Reference", new ModeReferenceFormatter());
            AddMode(ConsoleKey.G,  "Game Details", new ModeDetailedGameFormatter());
            AddMode(ConsoleKey.H,  "Baseball History", new ModeHistoryFormatter());
            AddMode(ConsoleKey.J,  "Injuries", new ModeHistoryFormatter());
            AddMode(ConsoleKey.L,  "Game Lineups", new ModeLineupFormatter());
            AddMode(ConsoleKey.N,  "News", new ModeNewsFormatter());
            AddMode(ConsoleKey.O,  "Overview", new ModeOverviewFormatter());
            AddMode(ConsoleKey.R,  "Transactions", new ModeTransactionsFormatter());
            AddMode(ConsoleKey.T,  "Teams", new ModeTeamsFormatter());
            AddMode(ConsoleKey.M,  "Team", new ModeTeamFormatter());
            AddMode(ConsoleKey.Z,  "Home", new ModeHomeFormatter());
            AddMode(ConsoleKey.W,  "Twitter", new ModeTwitterFormatter());

            CurrentMode = GetMode(ConsoleKey.O);
        }

        public static void ManageArrowChange()
        {
            if (m_currentMode.AssociatedKey != ConsoleKey.G)
            {
                m_currentMode = GetMode(ConsoleKey.G);
            }
        }

        /// <summary>
        /// Get the mode associated to the specified key
        /// </summary>
        public static Mode GetMode(ConsoleKey key)
        {
            if (m_list.ContainsKey(key))
            {
                return m_list[key];
            }
            else
            {
                return null;
            }
        }

    }


}
